package com.flyduck.manager;

import com.flyduck.entity.SysMenu;
import com.flyduck.entity.SysRole;
import com.flyduck.mapper.SysMenuMapper;
import com.flyduck.mapper.SysRoleMapper;
import com.flyduck.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * SysPermissionManager
 * </p>
 *
 * @author flyduck
 * @since 2024-05-29
 */
@Component
public class SysPermissionManager {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;

    public List<String> getPermissionListByUserId(Long userId) {
        //角色（ROLE_admin,ROLE_normal）
        List<SysRole> sysRoleList = sysRoleMapper.getRoleListByUserId(userId);
        List<String> roleCodeList = sysRoleList.stream()
                .map(sysRole -> {
                    return "ROLE_" + sysRole.getCode();
                }).collect(Collectors.toList());


        //菜单操作权限（sys:user:list）
        List<SysMenu> sysMenuList = sysMenuMapper.getMenuListByUserId(userId);
        List<String> permsList = sysMenuList.stream()
                .map(SysMenu::getPerms)
                .collect(Collectors.toList());

        roleCodeList.addAll(permsList);
        return roleCodeList;
    }
}
